<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/gf/common_alert.js"></script>
</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});

    Ext.require([
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.toolbar.Paging',
        'Ext.ModelManager',
        'Ext.tip.QuickTipManager'
    ]);

    var dataForm;
    var data = {};
    Ext.onReady(function () {

    	var product_id;
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams.product_id){
            product_id = _urlparams.product_id;
        }

        //保存数据s
        function doAdd(){
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
                return;
            }
            var params = dataForm.form.getValues();
            params.product_id = product_id;
            var _url = '/product/net-add';
            if(params && params._id ){
                _url = '/product/net-edit';
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    var data = Ext.decode(response.responseText);
                    if(data.flag == "success") {
                        alert_msg_refresh_close_win('业务处理成功！');
                        //alert_msg('业务处理成功！');
                    }else {
                        alert_msg('业务处理失败！'+data.msg);
                    }
                },
                failure: function(response, opts) {
                    alert_msg('业务处理失败！');
                }
            });

        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];
        dataForm = new Ext.FormPanel({
            layout : {
            	type: 'table',
            	columns: 1 
            },
            defaultType: 'textfield',
            fieldDefaults: {
	    		msgTarget: 'side',
	            labelWidth: 120,
	            labelAlign: 'right'
	        },
            items: [
                {
                    name : '_id',
                    hidden : true
                },{
                    fieldLabel: '单位净值',
                    labelWidth:120,
                    name : 'unit_net',
                    allowBlank: false
                    //value: 1.897
                },{
                    fieldLabel: '累计净值',
                    labelWidth:120,
                    name : 'total_net',
                    allowBlank: true//,
                    //value: 1.897
                },{
                	fieldLabel: '净值日期',
                	xtype: 'datefield',
                	format: 'Y-m-d',
                	name: 'net_date'//,
                	//value: new Date()
                }
            ],
            tbar : tbar
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });

        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/product/net-detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    //console.log(data);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                	if(i == 'net_date')
                		dataForm.form.findField(i).setValue(new Date(data[i]));	
                    else
                    	dataForm.form.findField(i).setValue(data[i]);
                }
            }
        }
    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>